var tablestudent_one_loaded = false;

/* Displays the details about the student of the given row. 
*/
function display_student_after_table(student_row) {
	var l = $(student_row).children()[0].childNodes[1];
	var address = l.attributes['href'].value;
	$("#student_box").load(address+"?ajax=true", null, function(){
		ping_machines();
	   $.ajax({
  			type: "GET",
			url: "/javascripts/student_detail.js",
  			dataType: "script"
		});
	});
}

/* To apply on each row of the student table
	Make it possible to click on rows to display student details
*/
function hijax_student_row() {
	$(this).click(function(){
		display_student_after_table(this);
		return false;
	});
}


$(document).ready(function() {

	/* Make the table searchable */
	$('table#table_students tbody tr').quicksearch({
		position: 'before',
		attached: 'table#table_students',
		stripeRowClass: ['odd', 'even'],
		labelText: 'Filter: ',
		loaderImg: '/img/busy.gif',
		delay: 250,
		focusOnLoad: true,
		onAfter: function () {
		  var rows = $(".student_row").filter(":visible");
		  if (rows.length == 1 && !tablestudent_one_loaded) {
		    tablestudent_one_loaded = true;
		    display_student_after_table(rows[0]);
		  }
		  else if (rows.length > 1 && tablestudent_one_loaded) {
		  	 tablestudent_one_loaded = false;
		  	 $("#student_box").html("");
		  }
		},
	});
	
	$('table#table_students tbody tr').each(hijax_student_row);
	loadjscssfile('/stylesheets/table_hijaxed.css', 'css')
	
});